<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="支付方式【'微信','支付宝'】" prop="payMode">
      <el-input v-model="dataForm.payMode" placeholder="支付方式【'微信','支付宝'】"></el-input>
    </el-form-item>
    <el-form-item label="支付公司方提供的流水号" prop="serialNum">
      <el-input v-model="dataForm.serialNum" placeholder="支付公司方提供的流水号"></el-input>
    </el-form-item>
    <el-form-item label="订单状态【0新建未支付，1已支付，2申请退款，3已退款】" prop="status">
      <el-input v-model="dataForm.status" placeholder="订单状态【0新建未支付，1已支付，2申请退款，3已退款】"></el-input>
    </el-form-item>
    <el-form-item label="课程id" prop="courseId">
      <el-input v-model="dataForm.courseId" placeholder="课程id"></el-input>
    </el-form-item>
    <el-form-item label="课程类型【0录播课，1直播课】" prop="courseType">
      <el-input v-model="dataForm.courseType" placeholder="课程类型【0录播课，1直播课】"></el-input>
    </el-form-item>
    <el-form-item label="支付价格" prop="payPrice">
      <el-input v-model="dataForm.payPrice" placeholder="支付价格"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除位【0删除，1存在】" prop="showStatus">
      <el-input v-model="dataForm.showStatus" placeholder="逻辑删除位【0删除，1存在】"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="自定义订单号" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="自定义订单号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          payMode: '',
          serialNum: '',
          status: '',
          courseId: '',
          courseType: '',
          payPrice: '',
          showStatus: '',
          createTime: '',
          updateTime: '',
          orderId: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          payMode: [
            { required: true, message: '支付方式【微信,支付宝】不能为空', trigger: 'blur' }
          ],
          serialNum: [
            { required: true, message: '支付公司方提供的流水号不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态【0新建未支付，1已支付，2申请退款，3已退款】不能为空', trigger: 'blur' }
          ],
          courseId: [
            { required: true, message: '课程id不能为空', trigger: 'blur' }
          ],
          courseType: [
            { required: true, message: '课程类型【0录播课，1直播课】不能为空', trigger: 'blur' }
          ],
          payPrice: [
            { required: true, message: '支付价格不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '逻辑删除位【0删除，1存在】不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '自定义订单号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/modules/courseorder/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.courseOrder.userId
                this.dataForm.payMode = data.courseOrder.payMode
                this.dataForm.serialNum = data.courseOrder.serialNum
                this.dataForm.status = data.courseOrder.status
                this.dataForm.courseId = data.courseOrder.courseId
                this.dataForm.courseType = data.courseOrder.courseType
                this.dataForm.payPrice = data.courseOrder.payPrice
                this.dataForm.showStatus = data.courseOrder.showStatus
                this.dataForm.createTime = data.courseOrder.createTime
                this.dataForm.updateTime = data.courseOrder.updateTime
                this.dataForm.orderId = data.courseOrder.orderId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/modules/courseorder/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'payMode': this.dataForm.payMode,
                'serialNum': this.dataForm.serialNum,
                'status': this.dataForm.status,
                'courseId': this.dataForm.courseId,
                'courseType': this.dataForm.courseType,
                'payPrice': this.dataForm.payPrice,
                'showStatus': this.dataForm.showStatus,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'orderId': this.dataForm.orderId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
